/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

public class Definiens  extends ItemDefinition  {

	private Definiendum definiendum = null;
	private Loci loci = null;
	private Visible visible = null;
	private Assumptions assumptions = null;
	private ProperDefiniens properDefiniens = null;
	
	private Definiens(){
		super(null);
	}

    public Definiens(Definiendum definiendum, Loci loci, Visible visible, Assumptions assumptions, ProperDefiniens properDefiniens) throws DliException {
    	super(null);
		this.definiendum = definiendum;
		this.loci = loci;
		this.visible = visible;
		this.assumptions = assumptions;
		this.properDefiniens = properDefiniens;
    }
	
	public static Definiens newInstance(String string) throws DliParseException {
		Definiens definiens = new Definiens();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in Definiens");
		}
		if (!string.substring(0, position).equals("$definiens")) {
			throw new DliParseException("["+string+"] is not valid in Definiens");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in Definiens");
		}
		String type = null;
		while (positionComma != -1) {
			if (string.startsWith("$definiendum")){
				definiens.definiendum = Definiendum.newInstance(string.substring(0, positionComma));
			} else if (string.startsWith("$loci")){
				definiens.loci = Loci.newInstance(string.substring(0, positionComma));
			} else if (string.startsWith("$visible")){
				definiens.visible = Visible.newInstance(string.substring(0, positionComma));
			} else if (string.startsWith("$assumptions")){
				definiens.assumptions = Assumptions.newInstance(string.substring(0, positionComma));
			} else if (string.startsWith("$proper_definiens")){
				definiens.properDefiniens = ProperDefiniens.newInstance(string.substring(0, positionComma));
			} else {
				throw new DliParseException("["+string+"] is not valid in Definiens");
			}
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in Definiens");
		}
		type = string.substring(0, position);
		if (type.equals("$definiendum")){
			definiens.definiendum = Definiendum.newInstance(string);
		} else if (type.equals("$loci")){
			definiens.loci = Loci.newInstance(string);
		} else if (type.equals("$visible")){
			definiens.visible = Visible.newInstance(string);
		} else if (type.equals("$assumptions")){
			definiens.assumptions = Assumptions.newInstance(string);
		} else if (type.equals("$proper_definiens")){
			definiens.properDefiniens = ProperDefiniens.newInstance(string);
		} else {
			throw new DliParseException("["+string+"] is not valid in Definiens");
		}
		
		return definiens;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("$definiens(");
		if (definiendum != null){
			sb.append(definiendum.toString());
		}
		if (loci != null){
			sb.append(",");
			sb.append(loci.toString());
		}
		if (visible != null){
			sb.append(",");
			sb.append(visible.toString());
		}
		if (assumptions != null){
			sb.append(",");
			sb.append(assumptions.toString());
		}
		if (properDefiniens != null){
			sb.append(",");
			sb.append(properDefiniens.toString());
		}
		sb.append(")");
		return sb.toString();
	}
}
